﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace WindowsGame2
{
    class Boy
    {
        Texture2D boy;
        Texture2D life;
        //Rectangle rec;
        Rectangle screen;
        Vector2 Position;
        Vector2 Velocity;
        float speed;
        float FrameTime = (float)1 /7;
        float TotalTime = 0;
        int spriteWidth, spriteHeight;
        int row = 0;
        int col = 0;
        int lifeCount = 3;
        int lifeDiamention = 32;
        SoundEffectInstance sound;
        public State BoyState;
        KeyboardState keyboardState;

        public enum State
        {
            Walking,
            Stoped,
            Dead
        }

        Rectangle Source
        {
            get
            {
                return new Rectangle(spriteWidth * col, spriteHeight * row, spriteWidth, spriteHeight);
            }
        }
        Rectangle Box
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, spriteWidth, spriteHeight);
            }
        }

        public Boy(Texture2D boy, Rectangle screen , Texture2D life , SoundEffectInstance sound)
        {
            this.boy = boy;
            this.screen = screen;
            this.life = life;
            this.sound = sound;
            spriteHeight =  105;
            spriteWidth = 80;
            Position = new Vector2(0, screen.Height - spriteHeight);
            BoyState = State.Walking;
            speed = 1.5f;
            
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(boy, Position, Source, Color.White);
            for (int i = 0; i < lifeCount; i++)
               spriteBatch.Draw(life, new Vector2(i* lifeDiamention, 0), Color.White);
                
        }

        public void Update()
        {
            checkWalls();
            BoyState = State.Stoped;
            Velocity = Vector2.Zero;

            keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                row = 1;
                Velocity.X = -1;
                BoyState = State.Walking;
            }
            if (keyboardState.IsKeyDown(Keys.Right))
            {
                row = 2;
                Velocity.X = 1;
                BoyState = State.Walking;
            }
            if (keyboardState.IsKeyDown(Keys.Up))
            {
                row = 3;
                Velocity.Y = -1;
                BoyState = State.Walking;
            }
            if (keyboardState.IsKeyDown(Keys.Down))
            {
                row = 0;
                Velocity.Y = 1;
                BoyState = State.Walking;
            }


            Position += Velocity * speed;
        }

        private void checkWalls()
        {
            if (Position.X < 0)
            {
                Position.X = 0;
                Velocity.X *= -1;
            }
            if (Position.X + spriteWidth > screen.Width)
            {
                Position.X = screen.Width - spriteWidth;
                Velocity.X *= -1;
            }
            if (Position.Y < 0)
            {
                Position.Y = 0;
                Velocity.Y *= -1;
            }
            if (Position.Y + spriteHeight > screen.Height)
            {
                Position.Y = screen.Height - spriteHeight;
                Velocity.Y *= -1;
            }
        }

        public void CheckCollition(Rock[] rocks)
        {
            foreach (Rock r in rocks)
            {
                if (Box.Intersects(r.Box) && !r.Collided)
                {
                    sound.Play();
                    r.Collided = true;
                    lifeCount--;
                    if( lifeCount <= 0)
                        BoyState = State.Dead;
                    return;
                }
            }
        }
        public void UpdateFrame(float epldTime)
        {
            if (BoyState == State.Stoped || BoyState == State.Dead)
                return;

            TotalTime += epldTime;
            if (TotalTime > FrameTime)
            {
                col++;
                col = col % 4;
                TotalTime -= FrameTime;
            }
        }

        public void ReStart()
        {
            BoyState = State.Stoped;
            Position = new Vector2(0, screen.Height - spriteHeight);
            lifeCount = 3;
        }
    }
}
